package model.forview;

import java.util.ArrayList;

import model.wrap.structure.Structure;
import model.wrap.structure.StructureException;
import models.ConstantName;
import models.ConstantValue;

public class StructuresModelForView extends ContentForMainTemplate
{
    private ArrayList<Structure> structures;
    private String query = "";
    
    
    public StructuresModelForView(ArrayList<Structure> str, String query)
    {
	this.query = query;
	this.structures = str;
	setDefaultValues();
    }

    public int getStrSize()
    {
	return structures.size();
    }

    public String getStrTitle(int index)
    {
	return structures.get(index).getTitle();
    }

    public String getStrDescription(int index)
    {
	String result = structures.get(index).getDescription();
	int length = 55;
	if( structures.get(index).getDescription().length() > length)
	    result = structures.get(index).getDescription().substring(0, length) + " ...";
	return result;
    }

    public String getStrCreaterName(int index)
    {
	String fullName = "";
	try
	{
	    fullName = structures.get(index).getCreater().getFullName();
	}
	catch (StructureException e)
	{
	    fullName = "";
	}

	return fullName;
    }
    
    public String getStrCreaterEmail(int index)
    {
	String fullName = "";
	try
	{
	    fullName = structures.get(index).getCreater().getEmail();
	}
	catch (StructureException e)
	{
	    fullName = "";
	}

	return fullName;
    }
    
    public Long getStrId(int index)
    {
	return (structures.get(index).getId());
    }

    public void setDefaultValues()
    {
	String title = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_TITLE);
	title = title != null ? title : "Иерархии";
	String userName = "";
	String header = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_HEADER);
	header = header != null ? header : "Выбор иерархии";
	String footer = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_FOOTER);
	footer = footer != null ? footer : "Список всех иерархий";
	mainModelForView = new MainModelForView(title, userName, header, footer);
    }
    
    public Structure getStrucutre(int index)
    {
	return this.structures.get(index);
    }

    public String getQuery()
    {
	return this.query;
    }
}
